#include "StdAfx.h"
#include "XmlData.h"

#include "TinyXml/tinyxml.h"

#include "CharsetConvert.h"

CString GetXmlAttrString(const TiXmlElement* pElement,const char* pKey)
{
	const char * pValue = pElement->Attribute(pKey);
	if (pValue)
	{
		cu2ct(pKey);
	}

	return _T("");
}

int GetXmlAttrInt(const TiXmlElement* pElement,const char* pKey)
{
	const char * pValue = pElement->Attribute(pKey);
	if (pValue)
	{
		atoi(pKey);
	}

	return 0;
}


CXmlData::CXmlData(void)
{
}

CXmlData::~CXmlData(void)
{
}

#define TEMPIMPLEMENT(x) bool CXmlData::Parse##x(const string& szXml,x##Data& sRet) const\
{\
	return false;\
}\


#define BASICIMPLEMENT(x) bool CXmlData::Parse##x(const string& szXml,x##Data& sRet) const\
{\
	TiXmlDocument xmldoc;\
	const char *tmpstr = xmldoc.Parse(szXml.c_str());\
	if (true)\
	{\
		TiXmlNode *pRootNode = xmldoc.FirstChild("message");\
		if (NULL == pRootNode)\
		{\
			return false;\
		}\
		TiXmlNode *pBodyNode = pRootNode->FirstChild("body");\
		if (NULL == pBodyNode)\
		{\
			return false;\
		}\
		TiXmlElement * pResult = pBodyNode->FirstChildElement("result");\
		if (NULL == pResult)\
		{\
			return false;\
		}\
		sRet.strCode = cu2ct(pResult->Attribute("Code"));\
		sRet.strDescription = cu2ct(pResult->Attribute("Description"));\
		return true;\
	}\
	return false;\
}\



bool CXmlData::ParseUserSpaceProfileQuery(const string& szXml,UserSpaceProfileQueryData& sRet) const
{
	TiXmlDocument xmldoc;

	const char *tmpstr = xmldoc.Parse(szXml.c_str());
	if (true)
	{
		TiXmlNode *pRootNode = xmldoc.FirstChild("message");
		if (NULL == pRootNode)
		{
			return false;
		}
		TiXmlNode *pBodyNode = pRootNode->FirstChild("body");
		if (NULL == pBodyNode)
		{
			return false;
		}
		TiXmlNode *pSpaceMessageList = pBodyNode->FirstChild("spaceMessageList");
		if (NULL == pSpaceMessageList)
		{
			return false;
		}

		for (TiXmlNode * pChild = pSpaceMessageList->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
		{
			TiXmlElement *pElement= pChild->ToElement();

			D2_6_SpaceMessage sItem;
			sItem.strUUID = GetXmlAttrString(pElement,"UUID");
			sItem.strAppID = GetXmlAttrString(pElement,"AppID");
			sItem.nSpaceStatus = (D2_6_SpaceStatus)GetXmlAttrInt(pElement,"SpaceStatus");
			sItem.nTotalSpace = GetXmlAttrInt(pElement,"TotalSpace");
			sItem.nUsedSpace = GetXmlAttrInt(pElement,"UsedSpace");
			sItem.nLockedSpace = GetXmlAttrInt(pElement,"LockedSpace");
			sItem.strResultCode = GetXmlAttrString(pElement,"ResultCode");
			sItem.strDescription = GetXmlAttrString(pElement,"Description");
		
			sRet.vSpaceMessageList.push_back(sItem);
		}

		return true;
	}
	return false;
}

bool CXmlData::ParseISpaceUserContentQuery(const string& szXml,ISpaceUserContentQueryData& sRet) const
{
	TiXmlDocument xmldoc;

	const char *tmpstr = xmldoc.Parse(szXml.c_str());
	if (true)
	{
		TiXmlNode *pRootNode = xmldoc.FirstChild("message");
		if (NULL == pRootNode)
		{
			return false;
		}
		TiXmlNode *pBodyNode = pRootNode->FirstChild("body");
		if (NULL == pBodyNode)
		{
			return false;
		}
		TiXmlNode *pContents = pBodyNode->FirstChild("contents");
		if (NULL == pContents)
		{
			return false;
		}

		TiXmlElement* pEle = pContents->ToElement();
		sRet.strUUID = GetXmlAttrString(pEle,"UUID");
		sRet.strSPID = GetXmlAttrString(pEle,"SPID");
		sRet.strAppID = GetXmlAttrString(pEle,"AppID");
		sRet.nType = (D2_7_Type)GetXmlAttrInt(pEle,"Type");
		sRet.nTotalCount = GetXmlAttrInt(pEle,"TotalCount");
		sRet.nCount = GetXmlAttrInt(pEle,"Count");
		sRet.strResultCode = GetXmlAttrString(pEle,"ResultCode");
		sRet.strDescription = GetXmlAttrString(pEle,"Description");

		for (TiXmlNode * pChild = pContents->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
		{
			TiXmlElement *pElement= pChild->ToElement();

			D2_7_Content sItem;
			sItem.strOrderCode = GetXmlAttrString(pElement,"OrderCode");
			sItem.strChannelID = GetXmlAttrString(pElement,"ChannelID");
			sItem.strChannelName = GetXmlAttrString(pElement,"ChannelName");
			sItem.strProgramID = GetXmlAttrString(pElement,"ProgramID");
			sItem.strProgramName = GetXmlAttrString(pElement,"ProgramName");
			sItem.nRecordType = (D2_7_RecordType)GetXmlAttrInt(pElement,"RecordType");
			sItem.strBeginTime = GetXmlAttrString(pElement,"BeginTime");
			sItem.strEndTime = GetXmlAttrString(pElement,"EndTime");
			sItem.strCreateTime = GetXmlAttrString(pElement,"CreateTime");
			sItem.strUpdateTime = GetXmlAttrString(pElement,"UpdateTime");
			sItem.nStatus = GetXmlAttrInt(pElement,"Status");
			sItem.nSize = GetXmlAttrInt(pElement,"Size");
			sItem.strContentID =GetXmlAttrString(pElement,"ContentID");
			sItem.strContentName = GetXmlAttrString(pElement,"ContentName");
			sItem.nContentSearchType = (D2_7_ContentSearchType)GetXmlAttrInt(pElement,"ContentSearchType");
			sItem.strUrl = GetXmlAttrString(pElement,"Url");
			sItem.strImageFormat = GetXmlAttrString(pElement,"ImageFormat");
			sItem.strImageResolution = GetXmlAttrString(pElement,"ImageResolution");
			sItem.nFileType = (D2_7_FileType)GetXmlAttrInt(pElement,"FileType");

			sRet.vContents.push_back(sItem);
		}



		return true;
	}
	return false;
}

//bool CXmlData::ParseNpvrOrderRequest(const string& szXml,NpvrOrderRequestData& sRet) const
//{
//	TiXmlDocument xmldoc;
//
//	const char *tmpstr = xmldoc.Parse(szXml.c_str());
//	if (tmpstr)
//	{
//		TiXmlNode *pRootNode = xmldoc.FirstChild("message");
//		if (NULL == pRootNode)
//		{
//			return false;
//		}
//		TiXmlNode *pBodyNode = pRootNode->FirstChild("body");
//		if (NULL == pBodyNode)
//		{
//			return false;
//		}
//
//		TiXmlElement * pResult = pBodyNode->FirstChildElement("result");
//		if (NULL == pResult)
//		{
//			return false;
//		}
//		sRet.strCode = cu2ct(pResult->Attribute("Code"));
//		sRet.strDescription = cu2ct(pResult->Attribute("Description"));
//
//		return true;
//	}
//	return false;
//}

bool CXmlData::ParseUserAccountAuth(const string& szXml,UserAccountAuthData& sRet) const
{
	TiXmlDocument xmldoc;

	const char *tmpstr = xmldoc.Parse(szXml.c_str());
	if (true)
	{
		TiXmlNode *pRootNode = xmldoc.FirstChild("message");
		if (NULL == pRootNode)
		{
			return false;
		}
		TiXmlNode *pBodyNode = pRootNode->FirstChild("body");
		if (NULL == pBodyNode)
		{
			return false;
		}
		TiXmlNode *pResults = pBodyNode->FirstChild("results");
		if (NULL == pResults)
		{
			return false;
		}

		for (TiXmlNode * pChild = pResults->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
		{
			TiXmlElement *pElement= pChild->ToElement();

			CommonResult sItem;
			sItem.strCode = GetXmlAttrString(pElement,"Code");
			sItem.strDescription = GetXmlAttrString(pElement,"Description");
			sRet.vResults.push_back(sItem);
		}

		return true;

	}
	return false;
}

bool CXmlData::ParseContentAuthurlQuery(const string& szXml,ContentAuthurlQueryData& sRet) const
{

	TiXmlDocument xmldoc;

	const char *tmpstr = xmldoc.Parse(szXml.c_str());
	if (true)
	{
		TiXmlNode *pRootNode = xmldoc.FirstChild("message");
		if (NULL == pRootNode)
		{
			return false;
		}
		TiXmlNode *pBodyNode = pRootNode->FirstChild("body");
		if (NULL == pBodyNode)
		{
			return false;
		}
		TiXmlNode *pResult = pBodyNode->FirstChild("result");
		if (NULL == pResult)
		{
			return false;
		}

		TiXmlElement* pElement = pResult->ToElement();

		sRet.strCode = GetXmlAttrString(pElement,"Code");
		sRet.strDescription = GetXmlAttrString(pElement,"Description");
		sRet.strPlayUrl = GetXmlAttrString(pElement,"PlayUrl");
		sRet.nBreakPoint = GetXmlAttrInt(pElement,"breakPoint");

		return true;
	}
	return false;
}

bool CXmlData::ParseISpaceUserTaskQuery(const string& szXml,ISpaceUserTaskQueryData& sRet) const
{

	TiXmlDocument xmldoc;

	const char *tmpstr = xmldoc.Parse(szXml.c_str());
	if (true)
	{
		TiXmlNode *pRootNode = xmldoc.FirstChild("message");
		if (NULL == pRootNode)
		{
			return false;
		}
		TiXmlNode *pBodyNode = pRootNode->FirstChild("body");
		if (NULL == pBodyNode)
		{
			return false;
		}
		TiXmlNode *pResults = pBodyNode->FirstChild("tasks");
		if (NULL == pResults)
		{
			return false;
		}

		TiXmlElement *pEle = pResults->ToElement();

		sRet.strUUID = GetXmlAttrString(pEle,"UUID");
		sRet.strSPID = GetXmlAttrString(pEle,"SPID");
		sRet.strAppID = GetXmlAttrString(pEle,"AppID");
		sRet.nTasksType = (D2_15_Type)GetXmlAttrInt(pEle,"Type");
		sRet.nCount = GetXmlAttrInt(pEle,"Count");
		sRet.strResultCode = GetXmlAttrString(pEle,"ResultCode");
		sRet.strDescription = GetXmlAttrString(pEle,"Description");


		for (TiXmlNode * pChild = pResults->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
		{
			TiXmlElement *pElement= pChild->ToElement();

			D2_15_TaskInfo sItem;
			sItem.strOrderCode = GetXmlAttrString(pElement,"OrderCode");
			sItem.nContentType = (D2_15_ContentType)GetXmlAttrInt(pElement,"ContentType");
			sItem.strChannelID = GetXmlAttrString(pElement,"ChannelID");
			sItem.strChannelName = GetXmlAttrString(pElement,"ChannelName");
			sItem.strProgramID = GetXmlAttrString(pElement,"ProgramID");
			sItem.strProgramName = GetXmlAttrString(pElement,"ProgramName");
			sItem.nRecordType = (D2_15_RecordType)GetXmlAttrInt(pElement,"RecordType");
			sItem.strBeginTime = GetXmlAttrString(pElement,"BeginTime");
			sItem.strEndTime = GetXmlAttrString(pElement,"EndTime");
			sItem.strCreateTime = GetXmlAttrString(pElement,"CreateTime");
			sItem.strUpdateTime = GetXmlAttrString(pElement,"UpdateTime");
			sItem.nStatus = (D2_15_Status)GetXmlAttrInt(pElement,"Status");
			sItem.nSize = GetXmlAttrInt(pElement,"Size");
			sItem.strContentID = GetXmlAttrString(pElement,"ContentID");
			sItem.strContentName = GetXmlAttrString(pElement,"ContentName");

			sRet.vTasks.push_back(sItem);
		}
		return true;
	}
	return false;
}

bool CXmlData::ParseISpaceUserContentDelete(const string& szXml,ISpaceUserContentDeleteData& sRet) const
{
	CString strT = cu2ct(szXml.c_str());
	TiXmlDocument xmldoc;

	const char *tmpstr = xmldoc.Parse(szXml.c_str());
	if (true)
	{
		TiXmlNode *pRootNode = xmldoc.FirstChild("message");
		if (NULL == pRootNode)
		{
			return false;
		}
		TiXmlNode *pBodyNode = pRootNode->FirstChild("body");
		if (NULL == pBodyNode)
		{
			return false;
		}
		TiXmlNode *pResults = pBodyNode->FirstChild("result");
		if (NULL == pResults)
		{
			return false;
		}

		TiXmlElement* pEle = pResults->ToElement();
		sRet.strCode = GetXmlAttrString(pEle,"Code");
		sRet.strDescription = GetXmlAttrString(pEle,"Description");


		TiXmlNode *pFaildContents = pResults->FirstChild("FaildContents");
		if (NULL == pFaildContents)
		{
			return false;
		}
		
		for (TiXmlNode * pChild = pFaildContents->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
		{
			TiXmlElement *pElement= pChild->ToElement();

			CString strContentID = GetXmlAttrString(pElement,"contentID");
			sRet.vFaildContents.push_back(strContentID);
		}

		return true;
	}
	return false;
}

bool CXmlData::ParseISpaceContentPlayurlQuery(const string& szXml,ISpaceContentPlayurlQueryData& sRet) const
{

	CString strT = cu2ct(szXml.c_str());
	TiXmlDocument xmldoc;

	const char *tmpstr = xmldoc.Parse(szXml.c_str());
	if (true)
	{
		TiXmlNode *pRootNode = xmldoc.FirstChild("message");
		if (NULL == pRootNode)
		{
			return false;
		}
		TiXmlNode *pBodyNode = pRootNode->FirstChild("body");
		if (NULL == pBodyNode)
		{
			return false;
		}
		TiXmlNode *pResults = pBodyNode->FirstChild("result");
		if (NULL == pResults)
		{
			return false;
		}

		TiXmlElement* pEle = pResults->ToElement();
		sRet.strCode = GetXmlAttrString(pEle,"Code");
		sRet.strDescription = GetXmlAttrString(pEle,"Description");
		sRet.strContentId = GetXmlAttrString(pEle,"contentID");


		TiXmlNode * pPlayUrlList = pResults->FirstChild("PlayUrlList");
		if (NULL == pPlayUrlList)
		{
			return false;
				
		}

		for (TiXmlNode * pChild = pPlayUrlList->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
		{
			TiXmlElement *pElement= pChild->ToElement();
			D2_20_PlayUrl sItem;
			sItem.strCodeRate = GetXmlAttrString(pElement,"codeRate");
			sItem.strCodec = GetXmlAttrString(pElement,"codec");

			TiXmlElement* pUrl = pChild->FirstChildElement("url");
			if (pUrl)
			{
				sItem.sUrl.strUrl = cu2ct(pUrl->GetText());
			}

			sRet.vPlayUrlList.push_back(sItem);
		}


		return true;
	}
	return false;
}


//TEMPIMPLEMENT(ISpaceUserContentQuery)
TEMPIMPLEMENT(UploadContentSyncRequest)
TEMPIMPLEMENT(UploadContentSyncNotify)
BASICIMPLEMENT(NpvrOrderRequest)
BASICIMPLEMENT(AddContentToUserISpace)
//TEMPIMPLEMENT(UserAccountAuth)
//TEMPIMPLEMENT(ContentAuthurlQuery)
//TEMPIMPLEMENT(ISpaceUserTaskQuery)
//TEMPIMPLEMENT(ISpaceUserContentDelete)
//TEMPIMPLEMENT(ChannelScheduleQuery)
//TEMPIMPLEMENT(UserPlayHistoryQuery)
//TEMPIMPLEMENT(ISpaceContentDetailQuery)
//TEMPIMPLEMENT(ISpaceContentPlayurlQuery)